package cn.alterzhang.www.test.javabasic;

import cn.alterzhang.www.Entity.User;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangshuhan
 * @date 2019/2/22
 */
public class ShallowAndDeepClone {


    public static void main(String[] args) {
        User p = new User("zhang", 18);
        User p1 = (User) p.clone();
        User p2 = (User) p.deepClone(p);
        System.out.println("p address=" + System.identityHashCode(p) + ",p=" + p);
        System.out.println("p1 address=" + System.identityHashCode(p1) + ",p1=" + p1);
        System.out.println(ifDeepClone(p, p1));
        System.out.println("p2 address=" + System.identityHashCode(p2) + ",p1=" + p2);
        System.out.println(ifDeepClone(p, p2));
    }

    private static String ifDeepClone(User user1, User user2) {
        String name1 = user1.getName();
        String name2 = user2.getName();
        if (System.identityHashCode(name1) == System.identityHashCode(name2)) {
            return "浅拷贝";
        } else {
            return "深拷贝";
        }
    }
}
